package com.dotflat.shop.controller;

import com.dotflat.shop.dto.ShopResponse;
import com.dotflat.util.RemoteCallUtilAddon;
import com.matt.foundation.model.ErrorCode;
import com.tuo51.foudation.model.StatusResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/api/op/shop")
@Slf4j
@Api(tags = "内嵌商铺 - ")
public class APIShopController{

    @Value("${server.shop.url}")
    private String shopServerURL;

    @ApiOperation("获取最新的商品列表")
    @RequestMapping(value = "/goods_new", method = RequestMethod.GET)
    public StatusResponse searchNew(@RequestHeader(TICKET) String ticket) {

        ShopResponse result = RemoteCallUtilAddon.getObject(shopServerURL + "/goods/searchNew", null, null, ShopResponse.class, "获取商品列表失败");

        if (20000 == result.getCode()){
            return StatusResponse.success(result.getData());
        }

        return StatusResponse.error(ErrorCode.SSO_CALL_ERROR, result.getMsg());
    }

    /**
     * 查询热门商品
     *
     * @return
     */
    @ApiOperation("获取最热的商品列表")
    @RequestMapping(value = "/goods_hot", method = RequestMethod.GET)
    public StatusResponse searchHot(@RequestHeader(TICKET) String ticket) {

        ShopResponse result = RemoteCallUtilAddon.getObject(shopServerURL + "/goods/searchHot", null, null, ShopResponse.class, "获取商品列表失败");

        if (20000 == result.getCode()){
            return StatusResponse.success(result.getData());
        }

        return StatusResponse.error(ErrorCode.SSO_CALL_ERROR, result.getMsg());
    }
}
